package com.itheima.controller;

import com.itheima.common.PageResult;
import com.itheima.common.Result;
import com.itheima.pojo.Articles;
import com.itheima.pojo.User;
import com.itheima.service.ArticlesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/articles")
public class ArticlesController {

    @Autowired
    private ArticlesService articlesService;

    @Autowired
    private HttpServletRequest req;

    /**
     * //查询面试列表
     * @param page
     * @param pagesize
     * @param keyword
     * @param state
     * @return
     */

    @GetMapping
    public PageResult findPage(Integer page, Integer pagesize, String keyword,Integer state){
        return articlesService.findPage(page,pagesize,keyword,state);
    }



    /**
     * //修改面试技巧
     * @param articles
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Articles articles){
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        articles.setState(1);
        articles.setCreatorID(user.getId());//todo 用户id
        articlesService.add(articles);
        return new Result(true,articles.getId());
    }

    /**
     * //删除面试技巧
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        articlesService.deleteById(id);
        return new Result(true,id);
    }

    //设置面试技巧状态/articles/:id/:state

    @PostMapping("/{id}/{state}")
    public Result updateState(@PathVariable Integer id, @PathVariable Integer state){
         articlesService.updateState(id,state);
         return new Result(true,id);
    }

    /**
     * 修改面试技巧信息
     * @param id
     * @param articles
     * @return
     */
    @PutMapping("/{id}")
    public Result updateById(@PathVariable Integer id, @RequestBody Articles articles){
         articlesService.updateById(id,articles);
         return new Result(true,id);
    }

    /**
     * 根据id查询详细信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Articles findById(@PathVariable Integer id){
        return articlesService.findById(id);
    }
}
